package org.example.common.express;

import com.ql.util.express.IExpressContext;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * QLExpress 上下文定义类
 */
public class QLExpressContext extends HashMap<String, Object> implements IExpressContext<String, Object> {

    private final ApplicationContext applicationContext;

    public QLExpressContext(Map<String, Object> properties, ApplicationContext context) {
        super(properties);
        this.applicationContext = context;
    }

    @Override
    public Object get(Object name) {
        Object result = super.get(name);
        try {
            if (result == null && this.applicationContext != null && this.applicationContext.containsBean((String) name)) {
                result = this.applicationContext.getBean((String) name);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Object put(String name, Object object) {
        super.put(name, object);
        return object;
    }
}